package com.niiwoo.civet.account.service.local.openAccount2.listener;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.constant.MQConstant;
import com.niiwoo.civet.account.dao.entity.BankCard;
import com.niiwoo.civet.account.dao.entity.BankType;
import com.niiwoo.civet.account.dao.entity.OpenAccountRecord;
import com.niiwoo.civet.account.dao.mapper.BankCardMapperExt;
import com.niiwoo.civet.account.dao.mapper.BankTypeMapperExt;
import com.niiwoo.civet.account.enums.OpenAccountRecordStatusEnum;
import com.niiwoo.civet.account.enums.OpenAccountWayEnum;
import com.niiwoo.civet.account.service.local.openAccount2.dto.OpenAccountEvent;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
public class CardNiuOpenAccountListener implements OpenAccountListener {

    private static final List<OpenAccountWayEnum> INTERESTED_WAY_LIST = Arrays.asList(OpenAccountWayEnum.CARD_NIU_PERSON_BIND_BANK_CARD_REGISTER, OpenAccountWayEnum.CARD_NIU_ACTIVATE, OpenAccountWayEnum.CARD_NIU_GROUP_ACCOUNT_CORRELATE);

    private static final List<OpenAccountRecordStatusEnum> EXCEPT_STATUS = Collections.singletonList(OpenAccountRecordStatusEnum.SUCCESS);

    @Autowired
    BankCardMapperExt bankCardMapperExt;

    @Autowired
    BankTypeMapperExt bankTypeMapperExt;

    @Autowired
    PrivacyMasks privacyMasks;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @Override
    public void handle(OpenAccountEvent event) {
        OpenAccountRecord record = event.getRecord();
        boolean isBorrowAccount = UserRoleEnum.enumOf(record.getAccountRole()) == UserRoleEnum.BORROWERS;
        if (!isBorrowAccount) {
            return;
        }
        BankCard bankCard = bankCardMapperExt.selectSimpleUserBankCard(record.getUserId());
        BankType bankType = bankTypeMapperExt.selectBankTypeByCode(bankCard.getBankCode());
        CardNiuMQ mq = new CardNiuMQ();
        mq.setUserId(record.getUserId());
        mq.setBankCardNo(privacyMasks.decryptPrivacy(bankCard.getBankAccountNo()));
        mq.setBankCardMobileNo(privacyMasks.decryptPrivacy(bankCard.getMobileNo()));
        mq.setBankName(bankType.getBankName());
        mq.setOrderId(String.valueOf(record.getId()));
        String json = JSON.toJSONString(mq);
        log.info("卡牛开户回调###data={}", json);
        rabbitTemplate.convertAndSend(MQConstant.CARD_NIU_ACCOUNT_EXCHANGE, MQConstant.CARD_NIU_OPEN_ACCOUNT_ROUTING, json);
    }

    @Override
    public boolean isExpectStatus(OpenAccountRecordStatusEnum status) {
        return EXCEPT_STATUS.contains(status);
    }

    @Override
    public boolean isInterestedOpenAccountWay(OpenAccountWayEnum way) {
        return INTERESTED_WAY_LIST.contains(way);
    }


    @Getter
    @Setter
    private static class CardNiuMQ implements Serializable {

        private static final long serialVersionUID = 2086740069815393982L;

        private String orderId;

        private String userId;

        private String bankCardNo;

        private String bankCardMobileNo;

        private String bankName;


    }

}
